from django.template import RequestContext
from django.shortcuts import redirect
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from hotels.models import Hotel, Room_serve, Room
from django.views.decorators.csrf import csrf_protect
from django.core.context_processors import csrf
from datetime import datetime
from models import Review
from django.contrib.auth.models import User
import MySQLdb

@login_required
def search(request):
    user = request.user
    context = {'user': user}
    context.update(csrf(request))

    return render_to_response('hotels/search.html',context)

@csrf_protect
@login_required
def booking(request):
    user = request.user
    context = RequestContext(request)
    rooms=[]
    checkin=''
    checkout=''

    if request.method == 'POST':
        hotelName = request.POST['hotel']
        checkin = request.POST['checkin']
        checkout = request.POST['checkout']
        country = request.POST['country']
        city = request.POST['city']
        type = request.POST['type']

        for element in Room.objects.raw('SELECT r.id,r.name,number FROM hotels_room r LEFT JOIN hotels_hotel h ON r.hotel_id = h.id WHERE h.name LIKE "%s" AND country LIKE "%s" AND city LIKE "%s" AND type LIKE "%s"'%(hotelName,country,city,type)):
            roomId = element.id
            roomName = element.name
            roomNumber = element.number
            for r in Room_serve.objects.raw('SELECT COUNT(*) AS c ,id FROM hotels_room_serve WHERE room_id = %d AND ( "%s" >= checkIn_date AND "%s" <= checkOut_date OR "%s" >= checkIn_date AND "%s" <= checkOut_date)'%(roomId,checkin,checkin,checkout,checkout)):
                res = r.c
            roomServedNumber = res
            roomNumber = roomNumber - roomServedNumber
            room=[roomId, roomName, roomNumber]
            rooms.append(room)

    return render_to_response('hotels/booking.html',{'rooms': rooms, 'checkin': checkin, 'checkout': checkout, 'user': user},context)

@csrf_protect
@login_required
def serve(request):
    user = request.user
    context = RequestContext(request)
    roomNumber = int(request.POST['roomnumber'])
    checkIn = datetime.strptime(request.POST['checkin'],'%Y-%m-%d')
    checkOut = datetime.strptime(request.POST['checkout'],'%Y-%m-%d')


    for x in range(0, roomNumber):
        room = Room.objects.get(pk=request.POST['roomid'])
        roomServe = Room_serve(room=room, checkIn_date=checkIn, checkOut_date=checkOut)
        roomServe.save()

    msg="You Have Succesffuly Rent %d Rooms"%(roomNumber)

    return redirect("/hotels/index/")

def index(request):
    if request.user.is_authenticated:
         user = request.user
    hotels = Hotel.objects.all()
    return render_to_response('hotels/index.html',{'hotels': hotels, 'user': user})

def view(request):
    if request.user.is_authenticated:
         user = request.user
    user = request.user
    hotelId = (request.GET['id'])
    if request.user.is_authenticated:
         user = request.user
    hotel = Hotel.objects.get(pk=hotelId)
    rooms = Room.objects.filter(hotel=hotel)
    reviews = Review.objects.filter(hotel=hotel)

    return render_to_response('hotels/view.html',{'user': user, 'hotel': hotel, 'rooms': rooms,'reviews': reviews, 'hotelId': hotelId, 'user': user})

def addComment(request):
    hotelId = request.GET['hotelId']
    userId = 1
    comment = request.GET['comment']
    rate = request.GET['rate']

    hotel = Hotel.objects.get(pk=hotelId)
    user = User.objects.get(pk=userId)

    review = Review(hotel=hotel, user=user, comment=comment, rate=rate)
    review.save()

    return HttpResponse("Done")
